package cn.edu.thu.laud.jdbc.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.zip.Deflater;

import org.apache.cassandra.thrift.Compression;

import com.google.common.base.Charsets;
/**
 *TYPE_* LaUDResult type. 
 *<br>
 */
public class LaUDJdbcUtils {
	
	public static final short TYPE_CASSANDRA=1;
	public static final short TYPE_HIVE=2;
	public static final short TYPE_OTHER=3;
	//trans file
	public static final short TYPE_FILE=4;
	/**
	 * LaUDResult type:when select commond 
	 */
	public static final short TYPE_MIX = 5;		
	/**
	 * FileInfo's direction :write into file system
	 */
	public static final char WRITE_INTO='w';
	/**
	 * FileInfo's direction :write into kv system
	 */
	public static final char WRITE_INTO_CASSANDRA='W';
	/**
	 * FileInfo's direction :read from filesystem
	 */
	public static final char READ_FROM='r';
	/**
	 * FileInfo's direction :read from kv system
	 */
	public static final char READ_FROM_CASSANDRA='R';
	
	/**
	 * FileInfo's direction :do nothing(it will occur when upload failed, the column_value is empty instead of a file path or file data.)
	 */
	public static final char OTHERS='O';

	

	   /**
     * Use the Compression object method to deflate the query string
     *
     * @param queryStr An un-compressed CQL query string
     * @param compression The compression object
     * @return A compressed string
     */
    public static ByteBuffer compressQuery(String queryStr, Compression compression)
    {
        byte[] data = queryStr.getBytes(Charsets.UTF_8);
        Deflater compressor = new Deflater();
        compressor.setInput(data);
        compressor.finish();

        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        try
        {
            while (!compressor.finished())
            {
                int size = compressor.deflate(buffer);
                byteArray.write(buffer, 0, size);
            }
        }
        finally
        {
            compressor.end(); //clean up after the Deflater
        }

       
        return ByteBuffer.wrap(byteArray.toByteArray());
    }
	
    public static  void writeIntoFile(InputStream instream,String filePath) throws IOException, SQLException{
		File file=new File(filePath);
		File parent=file.getParentFile();
		if(!parent.exists()){
			parent.mkdirs();
		}
		
		if(file.isDirectory()){
			throw new SQLException("file not folder..");
		}
		BufferedInputStream in = new BufferedInputStream(instream);        
		FileOutputStream out = new FileOutputStream(file);        

		//  System.out.println("Available bytes:" + in.available());        

		byte[] temp = new byte[4096];        
		int size = 0;        
		try{
			while ((size = in.read(temp)) != -1) {        
				out.write(temp, 0, size);        
			}
		}catch (IOException e) {
			throw e;
		}
		finally{
			try {
				in.close();
			} catch (IOException e) {
				in=null;
			}        
		}
		in.close();
		out.close();
	}
}
